package com.guc.coroutines

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.graphics.drawable.AnimationDrawable
import android.os.Bundle
import android.view.animation.AnimationUtils
import android.view.animation.RotateAnimation
import androidx.appcompat.app.AppCompatActivity
import com.guc.coroutines.databinding.ActivityAnimatorBinding
import com.guc.gviewbinding.inflate

class AnimatorActivity : AppCompatActivity() {
    private val binding: ActivityAnimatorBinding by inflate()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.btnStart.setOnClickListener {
            startAnim()
        }
    }

    @SuppressLint("ObjectAnimatorBinding")
    private fun startAnim() {
        //加载补间动画 （ alpha（淡入淡出），translate（位移），scale（缩放大小），rotate（旋转））
        val animation = AnimationUtils.loadAnimation(this, R.anim.alpha_anim)
        binding.iv1.startAnimation(animation)
//        binding.iv2.startAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate_anim))
        binding.iv2.apply {
            startAnimation(RotateAnimation(0f, 360f,width / 2f, height / 2f).apply {
                duration=2000
            })
        }

        //帧动画
        val animationDrawable = binding.iv3.drawable as AnimationDrawable
        animationDrawable.start()

        //属性动画
        val scaleXAnim = ObjectAnimator.ofFloat(binding.iv4,"scaleX",1f,2f).apply {
            repeatCount = ValueAnimator.INFINITE
        }
        val scaleYAnim = ObjectAnimator.ofFloat(binding.iv4,"scaleY",1f,2f).apply {
            repeatCount = ValueAnimator.INFINITE
        }
        val scaleAnim = AnimatorSet().apply {
            playTogether(scaleXAnim,scaleYAnim)
            duration = 1000
        }
        scaleAnim.start()

        //自定义属性动画
        binding.pointAnimView.startLineAnimator()

    }
}